<?php

$mFormRollback = new FormRecovery($_POST);
$mValidation = new InputValidation();
$mData = new DBQueryExecute();
$mFunction = new Fungsi();

$mData->SetTargerURL("main.php?menu=99&submenu=321");

$mValidation->SetInput($_POST['fAct']);
$mValidation->SetPattern(pAct);
$mAct = $mValidation->CheckValidation();


//SETUP PERSON
$mValidation->SetInput($_POST['fPersonId']);
$mValidation->SetPattern(pUserId);
$tUserId = $mValidation->CheckValidation();

//SETUP USERNAME
$mValidation->SetInput($_POST['fUsername']);
$mValidation->SetPattern(pUsername);
$tUsername = $mValidation->CheckValidation();

//SETUP LEVEL
$mValidation->SetInput($_POST['fOtorisasi']);
$mValidation->SetPattern(pUnsignedInt);
$tLevel = $mValidation->CheckValidation();

//SETUP DEFAULT PWD
//def password=tanggal lahir
$mPerson = new CorePerson($tUserId);
$tDefaultPwd = $mPerson->GetTanggalLahir();


//SETUP Nama Tabel
$mData->SetTableName("app_user");
$mData->SetMultiForm(false);

//$tGoto= "main.php?menu=11&submenu=111";
if ($mAct) {
    if ($mAct == 'add') {
        //SETUP LOGGED
        $mLogged = new Logged();
        $mLogged->SetUserId($tUserId);
        $mLogged->SetUsername($tUsername);
        $mLogged->SetPassword($tDefaultPwd);
        ////
        $tSalt = $mLogged->GenerateSalt();
        $tPwdHash = $mLogged->GeneratePasswordHash($tSalt);

        $mFunction->SetVariableToMultiFormVariableSupport('fSalt', $tSalt, false, false);
        $mFunction->SetVariableToMultiFormVariableSupport('fLoginSince', date("Y-m-d"), false, false);
        $mFunction->SetVariableToMultiFormVariableSupport('fPasswordHash', $tPwdHash, false, false);

        $mData->SetPrimaryKey('person_id', 'fPersonId', pUserId);
        $mData->SetField('username', 'fUsername', pUsername);
        $mData->SetField('pwd', 'fPasswordHash', pPass);
        $mData->SetField('level', 'fOtorisasi', pUsername);
        $mData->SetField('salt', 'fSalt', pUnsignedInt);
        $mData->SetField('login_s', 'fLoginSince', pDateDB);

        $mData->SetValiditas($tLevel . "|" . $tUsername . "|" . $tUserId . "|" . $tSalt);

        if (!$mData->GetError()) {
            $tResult = $mData->ExecuteQuery($mAct);
        }
    } else if ($mAct == "del") {
        $mData->SetPrimaryKey('person_id', 'fId', pUserId);
        $mData->SetField('person_id', 'fId', pUserId);
        if(!$mData->GetError())
            $tResult = $mData->ExecuteQuery($mAct);
    }
} else {
    if (!$mAct && $_POST['act']) {
        // ERROR: aksi tidak diketahui. kemungkinan diubah oleh user
    } else {
        // ERROR: nilai aksi tidak didefinisikan
    }
}
?>
